package com.rc.evcharger.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.evcharger.biz.utils.PubFetchTool;
import com.rc.evcharger.model.hlhtsk.HlhtskTenant;
import com.rc.evcharger.vo.cec.StdRequest;
import com.rc.evcharger.vo.charge.CommandStartReq;
import com.rc.evcharger.vo.charge.CommandStopReq;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * @Author: yang
 * @Date: 2022/6/16 16:55
 */
public class StartUtil {

    //启动
    private static String url = "http://localhost:8082/rc-evcharger/mini/query_start_charge";
    private static String url_3 = "http://47.106.210.3:8082/rc-evcharger/mini/query_start_charge";

    //停止
    private static String stopUrl = "http://localhost:8082/rc-evcharger/mini/query_stop_charge";
    private static String stopUrl_3 = "http://47.106.210.3:8082/rc-evcharger/mini/query_stop_charge";

    private RestTemplateUtil restTemplateUtil;

    public static void start() throws IOException {
        long l = System.currentTimeMillis() / 1000;
        CommandStartReq startChargeReq = new CommandStartReq();
        startChargeReq.setMemberId(5L);
//            startChargeReq.setCmdCode(SocketConstant.POWER_START);
        startChargeReq.setElecPileId(12582L);
        startChargeReq.setGunNum("2");
//        startChargeReq.setTicketUsingType(commandStartReq.getTicketUsingType());
        //外部订单号
        startChargeReq.setOutOrderNum(l + "");
        startChargeReq.setTenantCode("MA4T4JPE8");  //运营商
        startChargeReq.setCarNum("粤BFC0746");  //车牌号
        startChargeReq.setAccountType("0");
        //2,300 充电达到设定值     3，0.3充电电量达到设定值    1,300
        startChargeReq.setStartType("0");
        int send = 60;
        startChargeReq.setStartValue(send+"");

        //加密
        ObjectMapper objectMapper = new ObjectMapper();
        String respData = objectMapper.writeValueAsString(startChargeReq);
        String jsonData = encryptData(respData, "MA4T4JPE8");
        StdRequest tRequest = genSuccessReq(jsonData, "MA4T4JPE8");

        String jsonPrams = objectMapper.writeValueAsString(tRequest);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url_3);
        System.out.println("httpPost.getURI()..........................................." + httpPost.getURI());

        StringEntity entity = new StringEntity(jsonPrams, "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        System.out.println(httpEntity);
        String result = EntityUtils.toString(httpEntity, "utf-8");
        System.out.println("...........................................");
        System.out.println(result);
    }


    public static void stop() throws IOException {
        CommandStopReq commandStopReq = new CommandStopReq();
        commandStopReq.setTenantCode("MA4T4JPE8");
        commandStopReq.setMemberId(3L);
        commandStopReq.setElecStationId(10666l);
        commandStopReq.setElecPileId(12582L);
        commandStopReq.setElecGunId(13898L);
        commandStopReq.setGunNo("4401000100201");
        commandStopReq.setGunNum("1");
        commandStopReq.setOrderId(6547448L);
        //加密
        ObjectMapper objectMapper = new ObjectMapper();
        String respData = objectMapper.writeValueAsString(commandStopReq);
        String jsonData = encryptData(respData, "MA4T4JPE8");
        StdRequest tRequest = genSuccessReq(jsonData, "MA4T4JPE8");

        String jsonPrams = objectMapper.writeValueAsString(tRequest);

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(stopUrl_3);
        System.out.println("httpPost.getURI()..........................................." + httpPost.getURI());

        StringEntity entity = new StringEntity(jsonPrams, "UTF-8");
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        System.out.println(httpEntity);
        String result = EntityUtils.toString(httpEntity, "utf-8");
        System.out.println("...........................................");
        System.out.println(result);
    }

    protected static String encryptData(String respData, String tenantCode) {
        HlhtskTenant hlhtskTenant = new HlhtskTenant();
        hlhtskTenant.setDataSecret("badpyK9Shrim9Qer");
        hlhtskTenant.setDataSecretiv("qfC6il3PgOjyYZ4s");
        String data = PubFetchTool.genDDEncodeData(hlhtskTenant, respData);
        return data;
    }

    /**
     * 封装请求信息
     */
    protected static StdRequest genSuccessReq(String data, String tenantCode) {
        StdRequest resp = new StdRequest();
        resp.setData(data);
        resp.setSeq(tenantCode);
        return resp;
    }

    public static void main(String[] args) throws IOException {
//        start();
        stop();
    }
}
